package com.aw.baselib.util

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.View.OnFocusChangeListener
import android.widget.EditText
import com.aw.baselib.R

/**
 * Created by hzw
 */
object AwEditTextListener {
    fun setEditTextListener(context: Context, et: EditText, line: View) {
        et.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                line.setBackgroundColor(context.resources.getColor(R.color.red))
            } else {
                line.setBackgroundColor(context.resources.getColor(R.color.color_ebebeb))
            }
        }
    }

    @JvmStatic
    fun isChangeListener(et: EditText, callback: EditTextChangeListenerCallback) {
        et.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                if (s != null) {
                    callback.result(true)
                } else {
                    callback.result(false)
                }
            }
        })
    }

    interface EditTextChangeListenerCallback {
        fun result(changed: Boolean)
    }
}